package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserIdFavors;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserIdFavorsRedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IUserIdFavorsService;
import cn.wolfcode.wolf2w.util.DateUtil;
import cn.wolfcode.wolf2w.util.RidesKey;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class UserIdFavorsRedisServiceImpl implements IUserIdFavorsRedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private IUserIdFavorsService userIdFavorsService;

    @Override
    public boolean isUserIdFavorsExist(String usid) {
        String key = RidesKey.STRATEGY_USER_ID.join(usid);
        if (redisTemplate.hasKey(key)){
            return true;
        }
        return false;
    }

    @Override
    public void setUserIdFavorsRedis(UserIdFavors userIdFavors) {
        String key = RidesKey.STRATEGY_USER_ID.join(userIdFavors.getUsid());
        redisTemplate.opsForValue().set(key, JSON.toJSONString(userIdFavors));
    }

    @Override
    public List<UserIdFavors> findAllUserIdFavors() {
        String patten = RidesKey.STRATEGY_USER_ID.join("*");
        Set<String> keys = redisTemplate.keys(patten);
        ArrayList<UserIdFavors> userIdFavors = new ArrayList<>();
        for (String key : keys) {
            String favors = redisTemplate.opsForValue().get(key);
            userIdFavors.add(JSON.parseObject(favors,UserIdFavors.class));
        }
        return userIdFavors;
    }

    @Override
    public void setMongodb(List<UserIdFavors> userIdFavors) {
        for (UserIdFavors userIdFavor : userIdFavors) {
            userIdFavorsService.update(userIdFavor);
        }
    }
}

